package com.sjm.suchbear.minibookmall.action;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sjm.suchbear.minibookmall.model.NoticeModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Suchbear on 2018/5/5.
 */
public class NoticeManage {
    //新增公告
    public static void addNotice(SQLiteDatabase db, String noticetitle, String noticecontent) {
        ContentValues values = new ContentValues();
        values.put("noticetitle", noticetitle);
        values.put("noticecontent", noticecontent);
        db.insert("mall_notice", null, values);
    }

    //根据ID删除公告
    public static void deleteNotice(SQLiteDatabase db, String noticeID) {
        db.delete("mall_notice", "noticeID=?", new String[]{noticeID});
    }

    //获取公告列表
    public static List<NoticeModel> getNoticeList(SQLiteDatabase db) {
        List<NoticeModel> noticeModels = new ArrayList<>();
        Cursor cursor = db.query("mall_notice", new String[]{"noticeID", "noticetitle", "noticecontent"}, null
                , null, null, null, null);
        if (cursor.getCount() > 0) {
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToNext();
                NoticeModel noticeModel = new NoticeModel();
                noticeModel.setNoticeID(cursor.getString(0));
                noticeModel.setNoticetitle(cursor.getString(1));
                noticeModel.setNoticecontent(cursor.getString(2));
                noticeModels.add(noticeModel);
            }
            return noticeModels;
        }
        cursor.close();
        return null;
    }
}
